﻿#include <QMap>
#include <QVariant>
#include "track_data.h"
#include "constant_variable.h"

Track_data::Track_data(QObject *parent)
    : QObject(parent)
    , row_(0)
    , show_track_(false)
    , data_(new QMap<QString, QVariant>())
{

}

Track_data::~Track_data()
{

}

Track_data::Track_data(const Track_data &right_value)
    : QObject(nullptr)
{
    this->row_ = right_value.row_;
    this->show_track_ = right_value.show_track_;

    this->data_ = new QMap<QString, QVariant>();
    QMap<QString, QVariant>::iterator iter = right_value.data_->begin();

    while (right_value.data_->end() != iter) {
        this->data_->insert(iter.key(), iter.value());
        ++iter;
    }
}

Track_data &Track_data::operator =(const Track_data &right_value)
{
    if (this != &right_value) {
        this->row_ = right_value.row_;
        this->show_track_ = right_value.show_track_;
        this->data_ = new QMap<QString, QVariant>();

        QMap<QString, QVariant>::iterator iter = right_value.data_->begin();

        while (right_value.data_->end() != iter) {
            this->data_->insert(iter.key(), iter.value());
            ++iter;
        }
    }

    return *this;
}

void Track_data::set_row(int row)
{
    row_ = row;
}

int Track_data::get_row() const
{
    return row_;
}

void Track_data::set_show_track(bool show_track)
{
    show_track_ = show_track;
}

bool Track_data::get_show_track() const
{
    return show_track_;
}

void Track_data::setup_data(QString content)
{
    QString data_time(content.mid(0, 8));

    // boxing value variable as QVariant
    data_->insert(TIME.trimmed(), data_time.trimmed());

    QStringList list = content.mid(11).split(",");
    QStringList key_value_group;

    QStringList::Iterator iter;

    // Traverse list and save data to data_ variable
    for (iter = list.begin(); list.end() != iter; ++iter) {
        key_value_group = iter->split("=");

        if (2 == key_value_group.count()) {
            if (key_value_group[1].isEmpty()) {
                key_value_group[1] = "None";
            }

            // 将key/value保存到QMap中，去除数据的空格。将航班号的方括号去除
            data_->insert(key_value_group[0].trimmed(),
                    key_value_group[1].trimmed().replace("[", "").replace("]", ""));
        }
    }
}

QMap<QString, QVariant> *Track_data::get_data()
{
    return data_;
}
